package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 山西新闻网解析
 * @author Persh
 *
 */
public class SxrbNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.sxrb.com/[a-z]*/[a-z_0-9]*/[0-9_0-9]*/[0-9]*.shtml",};
//		"http://.*.sxrb.com/.news/[0-9]{6}/[0-9]*.html",
//		"http://.*.sxqic.com/news/[0-9]{6}/[0-9]*.html",
//		"http://.*.sxrb.com/news/[0-9]*/n[0-9]*.html",
//		"http://.*.sxrb.com/sxxww/.*/[0-9]+.shtml",
//		"http://it.sxrb.com/[0-9]{6}/[0-9]*b.shtml"};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("div.area6_left_box1>a.font16").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("div.area2_left_03").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("div.display_title").text();
					if(StringUtils.isBlank(title)){
						title =doc.select("div.left1>strong").text();
						if(StringUtils.isBlank(title)){
							title =doc.select("div.wzleft>div.w1").text();
							if(StringUtils.isBlank(title)){
								title =doc.select("div.nry_left>h1.nry_wzb").text();
								if(StringUtils.isBlank(title)){
									title =doc.select("a.font14").text();
									if(title==null || title.trim().length()==0){
										title =doc.select("td.font24s_000").text();
										if(StringUtils.isBlank(title)){
											title =doc.select(".art_tit").text();
											if(StringUtils.isBlank(title)){
												try {
													title =doc.select("div.content_title b").first().text();
												} catch (Exception e) {
												}
												if(StringUtils.isBlank(title)){
													title =doc.select("title").text();
													title=StringUtils.substringBefore(title, "_");
													if(StringUtils.isBlank(title)){
														title =doc.select("title").text();
														title=StringUtils.substringBefore(title, "-");
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("html body div.box div.area6 div.area6_left div.area6_left_box div.area6_left_box_1 font.font14_333 font"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select(".art_txt"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.area2_left_01>p"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div#row_Left>div.display_content"));
						if(StringUtils.isBlank(content)){
							content=HtmlCleaner.getContentHtml(url,doc.select("div.left8"));
							if(StringUtils.isBlank(content)){
								content=HtmlCleaner.getContentHtml(url,doc.select("div.w4>span>p"));
								if(StringUtils.isBlank(content)){
									content=HtmlCleaner.getContentHtml(url,doc.select("div.box>div.area6>div.area6_left>div.area6_left_box1>font"));
									if(StringUtils.isBlank(content)){
										content=HtmlCleaner.getContentHtml(url,doc.select("div.nry_zw"));
										if(content==null || content.trim().length()==0){
											content=HtmlCleaner.getContentHtml(url,doc.select("td.font14_000"));
											if(content==null || content.trim().length()==0){
												content=HtmlCleaner.getContentHtml(url,doc.select("#main_content"));
												if(content==null || content.trim().length()==0){
													content=HtmlCleaner.getContentHtml(url,doc.select("html body div.box div.area6 div.area6_left div.area6_left_box div.area6_left_box_1 font.font14_333"));
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			
			String tempdate=doc.select("div.area6_left_box_1,#time").text();
			if(StringUtils.isBlank(tempdate)){
				tempdate=doc.select("div.area2_left_01").text();
				if(StringUtils.isBlank(tempdate)){
					tempdate=doc.select("div#row_Left>div.Tags").text();
					if(StringUtils.isBlank(tempdate)){
						tempdate=doc.select("div.left2>span").text();
						if(StringUtils.isBlank(tempdate)){
							tempdate=doc.select("div.wzleft>div.w7>div").text();
							if(StringUtils.isBlank(tempdate)){
								tempdate=doc.select("span.zecc").text();
								if(StringUtils.isBlank(tempdate)){
									tempdate=doc.select("div.w7>div").text();
									if(tempdate==null || tempdate.trim().length()==0){
										tempdate=doc.select("span.w4.tr.link12_000").text();
										if(StringUtils.isBlank(tempdate)){
											tempdate=doc.select("td[height=30]").text();											
										}
									}
								}
							}
						}
					}
				}
			}
			
			if(StringUtils.isBlank(title)){
			}
			date=DateUtils.matchDate(tempdate);
			if(date==null){
				date=JSoupUtils.matchDate(doc, "时间：");
			}
			/**
			 * 解析用于更新的地址
			 */
			String updateUrl=null;
			String[] split=url.split("/");
			if(split[split.length-1].contains(".html")){
				String id=split[split.length-1].substring(0, split[split.length-1].indexOf("."));
				updateUrl="http://bbs.sxrb.com/api/comment/comment.php?id=" + id;
			}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);	
		
			String author=JSoupUtils.matchAuthor(doc,"来源：");
			news.setAuthor(author);
		return news;
	}
	
	public static void main(String[] args) {
		SxrbNewsAnalyse analyse=new SxrbNewsAnalyse();
		String url="http://www.sxrb.com/sxwb/aban_0/05_0/6393081.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return true;
	}
}
